﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using SilverlightCommands;
using System.ServiceModel;
using KMT_Pro.Web;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using KMT_Pro.Utils;


namespace KMT_Pro.ViewModels
{
    public class LoginViewModel : MessageSendableViewModel
    {
        public Visibility layoutRoot;

        public string txt_ID;
        public string txt_PASS;
        public string loginUserName;

        CustomerContext _context;        

        public LoginViewModel()
        {
            this.LoginClick = new RelayCommand((p) =>
            {
                _context = new CustomerContext();
                this.ServerRun(_context.Load(_context.SP_Login_CheckQuery(Txt_ID, Txt_PASS))
                    , (sender, e) =>
                        {
                            //비동기 이벤트 등록
                            try
                            {
                                LoadOperation lo = sender as LoadOperation;
                                foreach (SP_Login_Check_Result GAP in lo.AllEntities)
                                {
                                    if (GAP.Status.ToString() == "1")
                                    {
                                        LayoutRoot = Visibility.Collapsed;
                                        this.ServerRun(_context.Load(_context.GetUserMasterQuery())
                                        , (sender1, e1) =>
                                            {
                                                //비동기 이벤트 등록
                                                try
                                                {
                                                    LoadOperation lo1 = sender1 as LoadOperation;
                                                    foreach (UserMaster GAP1 in lo1.AllEntities)
                                                    {
                                                        if (GAP1.ID.Trim().Equals(this.Txt_ID))
                                                        {
                                                            ClientEnv.GetInstance().CurrentUser = GAP1;
                                                            LoginUserName = ClientEnv.GetInstance().CurrentUser.UserName.Trim();
                                                            MessageBox.Show(LoginUserName + "님 환영합니다.");
                                                        }
                                                    }
                                                }
                                                catch (Exception)
                                                { }
                                            });
                                    }
                                    else
                                    {
                                        MessageBox.Show("사용자 정보가 없습니다.");
                                    }
                                }
                            }
                            catch (Exception)
                            { }
                        });
            });
        }
      
        public ICommand LoginClick { get; set; }

        public Visibility LayoutRoot
        {
            get
            {
                return layoutRoot;
            }
            set
            {
                layoutRoot = value;
                OnPropertyChanged("LayoutRoot");
            }
        }

        public string Txt_ID
        {
            get
            {
                return txt_ID;
            }
            set
            {
                txt_ID = value;
                OnPropertyChanged("Txt_ID");
            }
        }

        public string Txt_PASS
        {
            get
            {
                return txt_PASS;
            }
            set
            {
                txt_PASS = value;
                OnPropertyChanged("Txt_PASS");
            }
        }

        public string LoginUserName
        {
            get
            {
                return loginUserName;
            }
            set
            {
                loginUserName = value;
                OnPropertyChanged("LoginUserName");
            }
        }
    }
}
